package com.example.CarManagement.sys.service.impl;

import com.example.CarManagement.handler.MyWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SendMessageService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private MyWebSocketHandler myWebSocketHandler;

    public void pushMessage(String userId, String message) {
        String sessionId = (String) redisTemplate.opsForValue().get(userId);
        if (sessionId != null) {

            myWebSocketHandler.sendToSession(sessionId, message);
        }
    }

    public void broadcast(String message) {
        myWebSocketHandler.broadcast(message);
    }
}
